//
//  CustomTabBar.m
//  lcf-newproject
//
//  Created by yangyang on 14-11-11.
//  Copyright (c) 2014年 yang. All rights reserved.
//

#import "CustomTabBar.h"

#define _UNSELECTEDIMAGE @"imagename"
#define _SELECTEDIMAGE @"selectedimagename"

static CustomTabBar * tabBarManager;
@implementation CustomTabBar

-(void)createTabBarWithFrame:(CGRect)frame tabBarDic:(NSDictionary *)dict{
    self.frame = frame;
    [self createTabBarWithDic:dict];
    self.backgroundColor = [UIColor lightGrayColor];
}

//根据字典创建自定义tabbar
-(void)createTabBarWithDic:(NSDictionary *)dict{
    if (dict.allKeys == 0) {
        return;
    }
    NSArray * imageArray = [dict objectForKey:_UNSELECTEDIMAGE];
    NSArray * selectedImageArray = [dict objectForKey:_SELECTEDIMAGE];
    CGFloat width = __TabBar_Width(imageArray.count);
    CGFloat height = __TabBar_Height+1;
    for(int i = 0;i<imageArray.count;i++){
        UIButton * btn = [LCFUIGlobel createButtonWithController:self WithFrame:CGRectMake(width*i, 0, width+1, height) withTitle:nil imageName:[imageArray objectAtIndex:i] selectedImageName:[selectedImageArray objectAtIndex:i] withAction:@selector(tabBarClick:)];
        //UIButton * btn = [LCFUIGlobel createButtonWithFrame:CGRectMake(width*i, 0, width, height) withTitle:nil imageName:[imageArray objectAtIndex:i] selectedImageName:[selectedImageArray objectAtIndex:i]];
        if (i == 0) {
            btn.selected = YES;
        }
        btn.tag = LCFTabBarTag+i;
        [self addSubview:btn];
    }
}
//更换button的selected状态
-(void)tabBarClick:(UIButton *)btn{
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            UIButton * currentBtn = (UIButton *)view;
            if (currentBtn.tag == btn.tag) {
                currentBtn.selected = YES;
            }else{
                currentBtn.selected = NO;
            }
        }
    }
    NSInteger index = btn.tag-LCFTabBarTag;
    if ([self.delegate respondsToSelector:@selector(customTabBar:didSelectedIndex:)]) {
        [self.delegate customTabBar:self didSelectedIndex:index];
    }
}
//隐藏标签栏控制器
-(void)hideTabBar:(BOOL)hidden{
    self.hidden = hidden;
}


@end
